var $;
var layer;
var table;
var form;
var upload;

    layui.use(['layer', 'table', 'form','upload'], function () {
        layer = layui.layer;
        $ = layui.jquery;
        table = layui.table;
        form = layui.form;
        upload=layui.upload;

        getclass();

        //初始化分类名称
        function getclass(){
            var id=$("#classId").val();
            $.post("/tea/selcourseClassName", {classId: id}, function (res) {
                if (res.code>-1) {
                    $("#className").val(res.obj);
                } else {
                    layer.msg("数据加载失败", {icon: 2});
                }
            });
        }

        //选择分类
        $("#classimg").on("click",function () {
            layer.open({
                type:2,
                title:"课程分类",
                area:['300px','400px'],
                shade:0.4,
                offset:'auto',
                content:"TeaCourseClass.html",
                success:function(layero,index) {},
                btn:['确定','取消'],
                yes:function(index,layero){
                    var obj=layero.find('iframe').contents();
                    var classId=obj.find("#classId").val();//课程分类
                    $("#classId").val(classId);
                    $.post("/tea/selcourseClassName", {classId: classId}, function (res) {
                        if(res){
                            $("#className").val(res.obj);
                            layer.close(index);
                        }else{
                            layer.msg("数据加载失败",{icon:2});
                        }
                    });
                },
                btn2:function (index,layero) {
                    layer.close(index);
                }
            });
        });

        //提交添加
        $("#subBtn").on("click",function () {
            var classId=$("#classId").val();
            var courseName=$("#courseName").val();
            var textsId=$("#textsId").val();
            var courseNprice=$("#courseNprice").val();
            if(classId && courseName && textsId && courseNprice){
                //获取dom对象
                var formdom=new FormData(document.getElementById("formId"));
                $.ajax({
                    url:"/tea/addCourse",
                    type:"post",
                    dataType:"json",
                    data:formdom,
                    contentType:false,//不转换为其他格式，是JS的form表单对象
                    processData:false,
                    success:function(data){
                        if (data.code>-1) {
                            layer.msg("课程添加成功", {icon: 1}, function(){
                                var layer_1=parent.layer_1;
                                layer_1.close(layer_1.index);
                            });
                        } else {
                            layer.msg("数据加载失败", {icon: 2});
                        }
                    }
                });
            }else{
                layer.msg("信息不全",{icon:2});
            }
        });

        //上传图片
            var uploadInst = upload.render({
                elem: '#getimg'
                , accept: 'images'
                , size: 5000 //限制文件大小，单位 KB
                , url: '/tea/addCourseImg' //上传接口
                , before: function (obj) {//预读本地文件示例
                    obj.preview(function (index, file, result) {
                        $('#imgdiv').attr('src', result); //图片链接（base64）
                    });
                    layer.msg('上传中', {icon: 16, time: 0});
                },
                done: function (res) {//上传完成的回调
                    if (res.code > -1) {
                        $("#url").val(res.obj);
                        layer.msg('上传完毕', {icon: 1});
                    } else {
                        layer.msg('上传失败,请重试', {icon: 2});
                    }
                },
                error: function () {//请求异常回调
                    layer.msg('上传出错，请重试', {icon: 2});
                }
            });


            //上传视频
        var uploadVideo = upload.render({//不能回显，可能上传得编码格式不对
            elem: '#getcourseVideo'
            , accept: 'video'
            , url: '/tea/addCourseVideo' //上传接口
            ,drag:true
            ,multiple:true
            , before: function (obj) {//预读本地文件示例
                obj.preview(function (index, file, result) {
                    var video=$('<video controls src="'+result+'" id="videId">');
                    $("#videdivId").html("").append(video);
                    $("#videdivId").removeClass("vidediv");
                });//视频回显
                layer.msg('上传中', {icon: 16, time: 0});
            },
            done: function (res) {//上传完成的回调
                if (res.code > -1) {
                    layer.msg('上传完毕', {icon: 1});
                    $("#courseUrl").val(res.obj);
                } else {
                    layer.msg('上传失败,请重试', {icon: 2});
                }
            },
            error: function () {//请求异常回调
                layer.msg('上传出错，请重试', {icon: 2});
            }
        });
    });